
<template>
  <div class="result">
    <van-nav-bar
      :title="keyword + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
      fixed
    ></van-nav-bar>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <van-cell
    @click="$router.push('/article/'+item.art_id)"
    v-for="item in list"
    :key="item.art_id"
    :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'SearchResult',
  computed: {
    keyword () {
      return this.$route.query.keyword
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad() {
      const res = await getSearch(this.page, this.keyword)
      this.list = [...this.list, ...res.data.results]
      // 加载完成要把loading 设置成false
      this.loading = false
      this.page++
      if (res.data.results.length === 0) {
        this.finished = true
      }
      // const res = await getSearch(this.page, this.keyword)

      // this.list = [...this.list, ...res.data.results]

      // // 加载完成。需要将loading置为false
      // this.loading = false
      // // 加载下一页数据
      // this.page++

      // if (res.data.results.length === 0) {
      //   this.finished = true
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.result {
  padding-top: 46px;
}.van-nav-bar {
  background-color: #3196fa;

  .van-nav-bar__title,
  .van-icon {
    color: rgb(255, 255, 255);
  }
}
</style>
